import request from '@/utils/request';
import moment from 'moment'
// 员工列表
export function staff(params) {
  const formOfEmploymentMapping = new Map([
    [1, '正式'],
    [2, '非正式'],
    [3, '未知']
  ])
  return request ({
    url: '/sys/user',
    method: 'get',
    params: {
      ...params
    }
  })
   .then(res => ({
    rows: res.data.data.rows.map((d, i) => ({
      ...d,
      index: (params.page - 1) * params.size + i + 1,
      formOfEmployment: formOfEmploymentMapping.get(d.formOfEmployment),
    })),
    total: res.data.data.total
  }))
   .catch(err => err)
}

// 给员工分配角色
export function role(roleIds, id) {
  return request ({
    url: '/sys/user/assignRoles',
    method: 'put',
    data: {
      roleIds,
      id,
    }
  })
    .then(res => res.data.message)
    .catch(err => Promise.reject(err.response.data.message))
}
// 新增员工
export function addStaff(data) {
  return request ({
    url: '/sys/user',
    method: 'post',
    data: {
      ...data,
    }
  })
    .then(res => res.data.message)
    .catch(err => Promise.reject(err.response.data.message))
}

export async function getAllEmployeeList() {
  const formOfEmploymentMapping = new Map([
    [1, '未知'],
    ['1', '未知'],
    [2, '正式'],
    [3, '非正式']
  ])

  const total = await request({
    url: '/sys/user',
    params: {
      page: 1,
      size: 10
    }
  })
    .then(res => res.data.data.total)

  return request({
    url: '/sys/user',
    params: {
      page: 1,
      size: total
    }
  })
    .then(res => ({
      rows: res.data.data.rows.map((d, i) => ({
        ...d,
        index: i + 1,
        formOfEmployment: formOfEmploymentMapping.get(d.formOfEmployment),
        timeOfEntry: moment(d.timeOfEntry).format('YYYY-MM-DD')
      })),
      total: res.data.data.total
    }))
    .catch(() => ({}))
}
